// pages/pay/pay.js 
const app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    agree: false,
    selectIdx: '0',
    text: '',
    list: [],
  },
  selectTypeTap: function (e) { //选择
    let that = this;
    let idx = e.currentTarget.dataset.idx;
    that.setData({
      selectIdx: idx
    })
  },
  agreeTap: function () { //同意协议
    let that = this;
    that.data.agree = !that.data.agree;
    that.setData({
      agree: that.data.agree
    })
  },
  openTips: function () { //查看协议
    wx.navigateTo({
      url: '/pages/rule/rule?tip=3',
    })
  },

  payTap: function () { //购买！
    let that = this;
    wx.requestSubscribeMessage({
      tmplIds: ['rTy3RHLVn-uV4uNpZE2sl2ukLTpSkAWd6TcSxTwgGCs'],
      success(res) {
        let temid='rTy3RHLVn-uV4uNpZE2sl2ukLTpSkAWd6TcSxTwgGCs'
        if(res[temid]=='accept'){
          that.pay(1)
        }else{
          that.pay(0)
        }
      }
    })
  },

  pay(check) {
    let that = this
    let {
      params,
      list,
      selectIdx
    } = that.data;
    wx.request({
      url: app.globalData.host + 'member/apply',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "POST",
      data: {
        checked: true,
        user_id: wx.getStorageSync('userId'),
        username: params.name,
        phone: params.phone,
        com_address: params.address,
        com_name: params.company,
        position: params.job,
        is_submit: params.is_submit,
        comarea: params.com_area,
        money: list[selectIdx].money,
        level_id: list[selectIdx].id,
        check: check
      },
      success(res) {
        wx.hideLoading();
        console.log(res)
        console.log(res.data.data.needpay)
        if (res.data.code == 1) {
          // 接支付 res.data.data.orderid
          if (!res.data.data.needpay) {
            wx.showToast({
              title: '提交成功，请等待审核',
              icon: 'none'
            })
          } else {
            wx.request({
              url: app.globalData.host + 'pay/paySociety',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              method: "POST",
              data: {
                orderid: res.data.data.orderid,
              },
              success(res1) {
                var res2 = res1.data
                wx.requestPayment({
                  'appId': res2.appId,
                  'timeStamp': res2.timeStamp,
                  'nonceStr': res2.nonceStr,
                  'package': res2.package,
                  'signType': res2.signType,
                  'paySign': res2.paySign,
                  'success': function (res) {
                    wx.showToast({
                      title: '支付成功',
                    })

                    wx.switchTab({
                      url: '/pages/mine/mine',
                    })
                    console.log(res);
                  },
                  'fail': function (res) {
                    wx.showToast({
                      title: '发起支付失败',
                      icon: "none"
                    })
                    console.log(res);
                  },
                  complete: function () {
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 1,
                      })
                    }, 1500)
                  }
                })
              }
            })
          }

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      },
      fail(err) {},
      complete(comp) {},
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var params = JSON.parse(options.params);
    that.setData({
      params,
    })
    console.log(params)
    wx.request({
      url: app.globalData.host + 'member/getMemberLevel',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      data: {
        // id: id
      },
      success(res) {
        wx.hideLoading();
        console.log(res)
        that.setData({
          list: res.data.data.list
          // article: res.data.data.detail,
          // text: WxParse.wxParse('text', 'html', res.data.data.detail.detail, that, 5),
        })
      },
      fail(err) {},
      complete(comp) {},
    })

    that.getRule(); //富文本
  },

  getRule() {
    var that = this;
    wx.request({
      url: app.globalData.host + 'society/getVipRule',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      data: {
        // id: id
      },
      success(res) {
        wx.hideLoading();
        console.log(res)
        that.setData({
          text: WxParse.wxParse('text', 'html', res.data.data.viprule.detail, that, 5),
        })
      },
      fail(err) {},
      complete(comp) {},
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})